#include <iostream>
#include <vector>

using namespace std;

/**
 * Definition for singly-linked list.
 */
struct ListNode 
{
    int val;
    ListNode * next;
    ListNode():val(0), next(nullptr)
    {}

    ListNode(int x):val(x), next(nullptr)
    {}

    ListNode(int x, ListNode * next):val(x), next(next)
    {}
};
 
class Solution 
{
public:
    ListNode* removeElements(ListNode* head, int val) 
    {
        ListNode * dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode * current = dummyHead;
        while(current->next != nullptr)
        {
            if(current->next->val == val)
            {
                ListNode * nodeToDelete = current->next;
                current->next = current->next->next;
                delete nodeToDelete;
                continue;
            }
            current = current->next;
        }
        ListNode * newHead = dummyHead->next;
        delete dummyHead;
        return newHead;
    }
};

// 辅助函数：根据数组创建链表
ListNode* createList(const vector<int>& nums) {
    ListNode dummy;
    ListNode* tail = &dummy;
    for (int num : nums) {
        tail->next = new ListNode(num);
        tail = tail->next;
    }
    return dummy.next;
}

// 辅助函数：打印链表
void printList(ListNode* head) {
    while (head) {
        cout << head->val;
        if (head->next) cout << "->";
        head = head->next;
    }
    cout << endl;
}

int main() {
    Solution sol;

    // 测试案例1
    vector<int> nums1 = {1, 2, 6, 3, 4, 5, 6};
    ListNode* head1 = createList(nums1);
    cout << "原链表1: ";
    printList(head1);
    ListNode* res1 = sol.removeElements(head1, 6);
    cout << "移除6后: ";
    printList(res1);

    // 测试案例2
    vector<int> nums2 = {7, 7, 7, 7};
    ListNode* head2 = createList(nums2);
    cout << "原链表2: ";
    printList(head2);
    ListNode* res2 = sol.removeElements(head2, 7);
    cout << "移除7后: ";
    printList(res2);

    return 0;
}
